package com.meixiang.fragment.service;


import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.DensityUtil;
import com.meixiang.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 滑动fragment
 * <p/>
 * xiejingwen
 */
public abstract class BaseViewPagerFragment extends BaseFragment {
    @Bind(R.id.ll_background)
    LinearLayout mLlBackground;
    @Bind(R.id.appbar)
    AppBarLayout mAppbar;
    private Context mContext;

    @Bind(R.id.ll_tab)
    LinearLayout llTab;
    @Bind(R.id.fl_mTabStrip_item)
    FrameLayout flMTabStripItem;
    @Bind(R.id.ll_mTabStrip)
    LinearLayout llMTabStrip;
    @Bind(R.id.viewpager)
    CustomViewPager viewpager;
    @Bind(R.id.view_style)
    View viewStyle;
    private String[] titles;
    private Fragment[] fragments;
    private List<TextView> viewList;

    //选中颜色
    private int ColorStyle;
    //未选中颜色
    protected int normalTextColor = R.color.black_8;
    ;

    protected abstract String[] getTitles();

    protected abstract Fragment[] getFragments();

    /**
     * 选中颜色
     */
    protected abstract int ColorStyle();

    private boolean isLine = true;
    private int background = R.color.white; //背景颜色

    private int textSize = 15;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getContext();
        View view = inflater.inflate(R.layout.fragment_base_view_pager, container, false);
        ButterKnife.bind(this, view);
        super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    protected void setTextNormalColor() {

    }

    /**
     *开启滑动隐藏tab
     * @return
     */
    public AppBarLayout startAppbar(){
        //app:layout_scrollFlags="scroll|exitUntilCollapsed"
        AppBarLayout.LayoutParams params= (AppBarLayout.LayoutParams) mLlBackground.getLayoutParams();
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL|AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        mLlBackground.setLayoutParams(params);
        return mAppbar;
    }

    /**
     * 设置是否有分割线
     *
     * @param isLine
     */
    protected void isCuttingLine(boolean isLine) {
        this.isLine = isLine;
    }

    /**
     * 设置背景颜色
     *
     * @param background 传入颜色ResId
     */
    protected void setBackground(int background) {
        this.background = background;
    }

    /**
     * 设置未选中字体颜色
     *
     * @param TextColor 传入颜色ResId
     */
    protected void setNormalTextColor(int TextColor) {
        this.normalTextColor = TextColor;
    }

    /**
     * 设置字体大小
     *
     * @param textSize
     */
    protected void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    /**
     * onCreateView 之后调用
     * 设置文字
     */
    protected void setTabText(int position, String text) {
        viewList.get(position).setText(text);
    }

    @Override
    protected void initView() {
        mLlBackground.setBackgroundResource(background);
        viewList = new ArrayList<>();
        titles = getTitles();
        ColorStyle = ColorStyle();
        fragments = getFragments();
        for (int i = 0; i < titles.length; i++) {
            if (i > 0 && isLine) {
                View view = new View(mContext);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.MATCH_PARENT);
                params.topMargin = DensityUtil.dip2px(mContext, 10);
                params.bottomMargin = DensityUtil.dip2px(mContext, 10);
                view.setLayoutParams(params);
                view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.line_color));
                llTab.addView(view);
            }
            TextView tv = new TextView(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
            tv.setLayoutParams(layoutParams);
            tv.setGravity(Gravity.CENTER);
            tv.setText(titles[i]);

//            tv.setTextColor(ColorStyle);
            //初始设置第一个为选中颜色
            if (i == 0) {
                tv.setTextColor(ContextCompat.getColor(mContext, ColorStyle));
            } else {
                tv.setTextColor(ContextCompat.getColor(mContext, normalTextColor));
            }
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

            final int finalI = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewpager.setCurrentItem(finalI);
                }
            });
            llTab.addView(tv);
            viewList.add(tv);
        }
        llMTabStrip.setWeightSum(titles.length);
        viewStyle.setBackgroundColor(ContextCompat.getColor(mContext, ColorStyle));
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(new PagerAdapter(fragments, getChildFragmentManager()));
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                flMTabStripItem.setTranslationX(viewList.get(position).getWidth() * position + positionOffsetPixels / viewList.size());
            }

            @Override
            public void onPageSelected(int position) {
                initColor();
                viewList.get(position).setTextColor(ContextCompat.getColor(mContext, ColorStyle));
                viewList.get(position).setTypeface(Typeface.DEFAULT_BOLD);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initColor() {
        for (TextView view : viewList) {
            view.setTextColor(ContextCompat.getColor(mContext, normalTextColor));
            view.setTypeface(Typeface.DEFAULT);
        }
    }

    @Override
    protected void setData() {

    }

    public void setViewpagerSlide(boolean isSlide) {
        viewpager.setScanScroll(isSlide);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class PagerAdapter extends FragmentPagerAdapter {
        private Fragment[] fragments;

        public PagerAdapter(Fragment[] fragments, FragmentManager fm) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }
}
